package com.fance.controller;

import com.fance.model.HostHolder;
import com.fance.model.Message;
import com.fance.model.User;
import com.fance.model.ViewObject;
import com.fance.service.MessageService;
import com.fance.service.UserService;
import com.fance.util.ToutiaoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author fance
 * @date 18-5-6 上午10:04
 */
@Controller
public class MessageController {
    private static final Logger logger = LoggerFactory.getLogger(MessageController.class);

    @Autowired
    HostHolder hostHolder;

    @Autowired
    MessageService messageService;

    @Autowired
    UserService userService;

    @RequestMapping(path = "/msg/list",method = RequestMethod.GET)
    public String conversationList(Model model) {

        try {
            int localUserId = hostHolder.getUser().getId();
            List<ViewObject> conversations = new ArrayList<>();
            List<Message> conversationList = messageService.getConversationList(localUserId,0,10);
            for (Message message : conversationList) {
                ViewObject viewObject = new ViewObject();
                viewObject.set("conversation",message);
                int targetId = message.getFromId() == localUserId ? message.getToId() : message.getFromId();

                User user = userService.getUser(targetId);
                viewObject.set("user", user);
                viewObject.set("unread", messageService.getConversationUnreadCount(localUserId,message.getConversationId()));

                conversations.add(viewObject);
            }
            model.addAttribute("conversations", conversations);
        } catch (Exception e) {
            logger.error("获取站内信列表失败" + e.getMessage());
        }
        return "letter";
    }

    @RequestMapping(path = {"/msg/detail"}, method = {RequestMethod.GET})
    public String conversationDetail(Model model, @RequestParam("conversationId") String conversationId) {
        try {
            List<Message> conversationList = messageService.getConversationDetail(conversationId, 0, 10);
            List<ViewObject> messages = new ArrayList<>();
            for (Message message : conversationList) {
                ViewObject viewObject = new ViewObject();
                viewObject.set("message",message);
                User user = userService.getUser(message.getFromId());
                if (user == null) {
                    continue;
                }
                viewObject.set("headUrl", user.getHeadUrl());
                viewObject.set("userId",user.getId());
                messages.add(viewObject);
            }
            model.addAttribute("messages", messages);
        } catch (Exception e) {
            logger.error("获取详情消息失败" + e.getMessage());
        }
        return "letterDetail";
    }

    @RequestMapping(path = {"/msg/addMessage"}, method = {RequestMethod.POST})
    @ResponseBody
    public String addMessage(@RequestParam("fromId") int fromId,
                             @RequestParam("toId")int toId,
                             @RequestParam("content")String content) {

        try {
            Message message = new Message();
            message.setContent(content);
            message.setToId(toId);
            message.setFromId(fromId);
            message.setCreatedDate(new Date());

            messageService.addMessage(message);
            return ToutiaoUtil.getJSONString(message.getId());
        } catch (Exception e) {
            logger.error("增加评论失败" + e.getMessage());
            return ToutiaoUtil.getJSONString(1, "插入评论失败");
        }
    }
}
